package com.fsck.k9.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fsck.k9.mail.Address;
import com.fsck.k9.view.tokenautocomplete.TokenCompleteTextView;
import com.yunshipei.enterplorer.R;

/**
 * Sample token completion view for basic contact info
 * <p/>
 * Created on 9/12/13.
 *
 * @author mgod
 */
public class ContactsCompletionView extends TokenCompleteTextView<Address> {

    public ContactsCompletionView(Context context) {
        super(context);
    }

    public ContactsCompletionView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ContactsCompletionView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected View getViewForObject(Address adress) {

        LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) l.inflate(R.layout.contact_token, (ViewGroup) ContactsCompletionView.this.getParent(), false);
        if (!TextUtils.isEmpty(adress.getPersonal())) {
            ((TextView) view.findViewById(R.id.name)).setText(adress.getPersonal());
        } else {
            ((TextView) view.findViewById(R.id.name)).setText(adress.getAddress());
        }

        return view;
    }

    @Override
    protected Address defaultObject(String completionText) {
        //Stupid simple example of guessing if we have an email or not
//        int index = completionText.indexOf('@');
//        if (index == -1) {
//            return new Address(completionText, completionText.replace(" ", "") + "@yunshipei.com");
//        } else {
//            return new Address(completionText.substring(0, index), completionText);
//        }
        return new Address(completionText, "");
    }
}
